//
//  WBNavViewController.swift
//  WeiBoApp
//
//  Created by James on 2017/6/11.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

class WBNavViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true
        
        // Do any additional setup after loading the view.
    }
    
    //pop返回上级控制器
    @objc fileprivate func popToBefore()  {
        popViewController(animated: true)
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0{
            viewController.hidesBottomBarWhenPushed = true
        }
        
        //判断类型
        if let vc = viewController as?WBBaseViewController {
            var title = "返回"
            var isBack = false
            
            if childViewControllers.count == 1{
                //只有一个子控制器的时候，显示栈低vc的标题
                title = childViewControllers.first?.title ?? "返回"
            }
            if childViewControllers.count > 1{
                isBack = true
            }
            
            //取出自定义的navitem 添加返回按钮
            vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, normalColor: UIColor.gray, highlightColor: UIColor.orange, target: self, action: #selector(popToBefore),isBack:isBack)
        }
        
        super.pushViewController(viewController, animated: animated)
    }
}
